﻿using WeiBeeCommon.Core;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using System.Collections.Specialized;
using System.Web;

namespace TestQunFaExpert
{
    
    
    /// <summary>
    ///This is a test class for WeiBeeSohuTest and is intended
    ///to contain all WeiBeeSohuTest Unit Tests
    ///</summary>
    [TestClass()]
    public class WeiBeeSohuTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Retweet
        ///</summary>
        [TestMethod()]
        public void RetweetTest()
        {
            WeiBeeSohu target = new WeiBeeSohu(); // TODO: Initialize to an appropriate value
            target.OAuth.Token = "5dcbab5bc6c95380f1c85df2f956ae9b";
            target.OAuth.TokenSecret = "f90dddc41002d822a83ed9915f23696f";
            string id = "2006109361";
            string status = string.Empty;
            string expected = string.Empty; // TODO: Initialize to an appropriate value
  //            <Stock>
  //  <Token>5dcbab5bc6c95380f1c85df2f956ae9b</Token>
  //  <TokenSecret>f90dddc41002d822a83ed9915f23696f</TokenSecret>
  //  <LastUpdateDateTime>2011-03-03T22:15:46.7851562+08:00</LastUpdateDateTime>
  //  <UserType>Sohu</UserType>
  //  <StockString>sh000001</StockString>
  //  <stockType>StockIndex</stockType>
  //</Stock>
            string actual;
            actual = target.Retweet(id, status);
            //Assert.AreEqual(expected, actual);
        }

        [TestMethod()]
        public void SilentOAuthTest()
        {
            var oAuth = new OAuthSohu();
            string username = "weibee009@sohu.com";
            string pwd = "hao123";
            oAuth.SetCallbackUrl(string.Empty);
            oAuth.AddXAuthParameters(username, pwd);
            string accessTokenUrl = "http://api.t.sohu.com/oauth/access_token";
            string response = oAuth.OAuthWebRequest(Method.POST, accessTokenUrl, String.Empty);
            NameValueCollection requestToken = HttpUtility.ParseQueryString(response);
            if (requestToken["oauth_token"] == null || requestToken["oauth_token_secret"] == null)
            {
                throw new Exception("didn't get access token, server response data:" + response);
            }
            else
            {
                Assert.IsNotNull(requestToken["oauth_token"]);
                Assert.IsNotNull(requestToken["oauth_token_secret"]);
                //save this token and secret, it will be used in the 3rd step
                //oAuth.AccessToken = requestToken[OAuthBase.OAuthTokenKey];
                //oAuth.AccessTokenSecret = requestToken[OAuthBase.OAuthTokenSecretKey];
            }
        }

        [TestMethod]
        public void GenerateSignatureTest()
        {
            var oAuth = new OAuthSohu();
            string username = "weibee901@sohu.com";
            string pwd = "hao123";
            oAuth.SetCallbackUrl(string.Empty);
            oAuth.AddXAuthParameters(username, pwd);
            Uri url = new Uri("http://api.t.sohu.com/oauth/access_token");
            string oauth_consumer_key = "AYByrZSXj5uaFKytTomy";
            string oauth_nonce = "197649345";
            OAuthBase.SignatureTypes oauth_signature_method = OAuthBase.SignatureTypes.HMACSHA1;// "HMAC-SHA1";
            string oauth_timestamp = "1318669525";
            //string oauth_version = "1.0";
            string oauth_consumer_secret = "kSIld=0ET$nUtRsWjUr(tl2-Q1l#C0G04Rr3cdgn";

            string httpMethod = "POST";

            string normalizedUrl;
            string normalizedRequestParameters;

            string expected = "cugJJvrd6alXRtM2MO8nrV/VEzA=";

            string result = oAuth.GenerateSignature(url, oauth_consumer_key, oauth_consumer_secret, string.Empty, string.Empty, string.Empty, httpMethod, oauth_timestamp, oauth_nonce, oauth_signature_method, out normalizedUrl, out normalizedRequestParameters);

            //Assert.AreEqual(expected, result);

            oauth_timestamp = "1318671153";
            oauth_nonce = "662604907";
            expected = "POST&http%3A%2F%2Fapi.t.sohu.com%2Foauth%2Faccess_token&oauth_consumer_key%3DAYByrZSXj5uaFKytTomy%26oauth_nonce%3D662604907%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1318671153%26oauth_version%3D1.0%26x_auth_mode%3Dclient_auth%26x_auth_password%3Dhao123%26x_auth_username%3Dweibee901%2540sohu.com";

            string HMACSHA1SignatureType = "HMAC-SHA1";
            result = oAuth.GenerateSignatureBase(url, oauth_consumer_key, string.Empty, string.Empty, string.Empty, httpMethod, oauth_timestamp, oauth_nonce, HMACSHA1SignatureType, out normalizedUrl, out normalizedRequestParameters);
            Assert.AreEqual(expected, result);
        }

        [TestMethod]
        public void XAuthLoginTest()
        {
            var sohu = new OAuthSohu();
            sohu.XAuthLogin("weibee901@sohu.com", "hao123");
            Assert.IsNotNull(sohu.Token);
        }

        [TestMethod]
        public void GetWeiBeeUserInfoSohuTest()
        {
            var weibeeSohu = new WeiBeeSohu();
            weibeeSohu.SetOAuth("b7e32d1a0b186e2a787917ab0388c998", "50ed5713988942f6139679e09beeb65f");
            var sohuUser = weibeeSohu.GetWeiBeeUser();
            Assert.IsNotNull(sohuUser);
        }
    }
}
